package com.yang.Hash_;

import java.util.HashSet;

public class LeetCode202 {
    public static void main(String[] args) {
        SolutionLeetCode202 solutionLeetCode202 = new SolutionLeetCode202();
        System.out.println(solutionLeetCode202.isHappy(19));
    }
}


// 解题关键！！！题目提醒了可能无线循环，那就说明了会重复，既然重复了那就肯定不是快乐数！！！！！
// 然后用Hashset依次检查添加 如果检查到重复那就是false 否则就会出现1的结果
class SolutionLeetCode202 {
    public boolean isHappy(int n) {
        HashSet<Integer> integers = new HashSet<>();
        while (integers.contains(n) != true){
            if ( n ==1 ){
                return true;
            }
            integers.add(n);
            n = sumAdd(n);
        }
        return false;
    }
    private int sumAdd(int n){
        int sum = 0;
        while (n != 0){
            int temp = n % 10;
            sum += temp * temp;
            n /= 10;
        }
        return sum;
    }
}
